//package com.work.formaldehyde.wxapi;
//
//import android.app.Activity;
//
//import androidx.appcompat.app.AlertDialog;
//
//import com.tencent.mm.opensdk.constants.ConstantsAPI;
//import com.tencent.mm.opensdk.modelbase.BaseResp;
//import com.work.formaldehyde.R;
//import com.work.formaldehyde.util.ApiUtils;
//
//public class WXPayEntryActivity extends Activity {
//    public void onResp(BaseResp baseResp){
//        if(baseResp.getType()== ConstantsAPI.COMMAND_PAY_BY_WX){
//            ApiUtils.SetLog("onPayFinish,errCode="+baseResp.errCode);
//            AlertDialog.Builder builder=new AlertDialog.Builder(this);
//            builder.setTitle(R.string.app_tip);
//        }
//    }
//}
package com.work.formaldehyde.wxapi;


import android.app.Activity;
import android.app.ActivityOptions;
import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.Switch;

import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;
import com.work.formaldehyde.R;
import com.work.formaldehyde.activity.OrderActivity;
import com.work.formaldehyde.homefragment.WodeFragment;
import com.work.formaldehyde.util.ApiUtils;
import com.work.formaldehyde.util.Url;

import org.json.JSONObject;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {

    private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";

    private IWXAPI api;

    public static boolean wxpay_s = false,wxpay_s1 = false;
    public static boolean wxclose=false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        api = WXAPIFactory.createWXAPI(this, Url.WXAPPID);
        api.handleIntent(getIntent(), this);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        api.handleIntent(intent, this);

    }

    @Override
    public void onReq(BaseReq baseReq) {

    }

    @Override
    public void onResp(BaseResp resp) {
        if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
            onBackPressed();
            switch (resp.errCode) {
                case -1:
                    if (Url.wxdingdan || Url.wxgwc)
                        intent_to_orderactivity(0);
                    wxclose=true;
                    ApiUtils.SetToast(WXPayEntryActivity.this, "支付失败");
                    break;
                case -2:
                    if (Url.wxdingdan || Url.wxgwc)
                        intent_to_orderactivity(0);
                    wxclose=true;
                    ApiUtils.SetToast(WXPayEntryActivity.this, "用户已取消");
                    break;
                case 0:
//                    getalipay();
                    if (Url.wxdingdan || Url.wxgwc)
                        intent_to_orderactivity(1);
                    wxpay_s = true;
                    wxpay_s1 = true;
                    wxclose=true;
                    ApiUtils.SetToast(WXPayEntryActivity.this, "支付成功");
                    break;
            }
        }
    }

    /**
     * 用int来判断是什么入口进入的0：待支付 1：待收货 2：待评价 3：全部订单
     *
     * @param id
     */
    public void intent_to_orderactivity(int id) {
        WodeFragment.sid = id;
        Intent order = new Intent(WXPayEntryActivity.this, OrderActivity.class);
        startActivity(order, ActivityOptions.makeSceneTransitionAnimation(WXPayEntryActivity.this).toBundle());
        Url.wxdingdan = false;
        Url.wxgwc = false;
    }


    private void getalipay() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient okHttpClient = new OkHttpClient();
                    RequestBody formBody = null;
                    formBody = new FormBody.Builder().add("paytype", "微信支付").add("order_number", Url.PAY_OREDNUM)
                            .build();
                    Request request = new Request.Builder().url(Url.PAYCSECSS).post(formBody).build();
                    okhttp3.Response response = okHttpClient.newCall(request).execute();
                    String msg = response.body().string();
                    Message message = new Message();
                    message.obj = msg;
                    getalihots.sendMessage(message);
                } catch (
                        Exception e) {
                    e.printStackTrace();
                }
            }
        }).
                start();
    }

    public Handler getalihots = new Handler() {
        public void handleMessage(Message msg) {
            JSONObject success_Object;
            try {
                if (msg.obj != null) {
                    success_Object = new JSONObject(msg.obj.toString());
                    String code = success_Object.getString("code");
                    if (code.equals("1")) {
                        wxpay_s = true;
                        wxpay_s1 = true;
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}